package com.aos.erp.manager.rest;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.manager.dto.NoticeDTO;
import com.aos.erp.manager.dto.NoticeDetailDTO;
import com.aos.erp.manager.model.notice.NoticeInfo;
import com.aos.erp.manager.model.notice.vo.NoticeConditionVo;
import com.aos.erp.manager.model.notice.vo.NoticeVo;
import com.aos.erp.manager.model.sys.dto.UserDto;
import com.aos.erp.manager.service.NoticeService;
import com.aos.erp.manager.util.UserUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Api(tags = {"UI接口：通知公告接口"})
@RestController
@RequestMapping("/api/notice")
public class RestNoticeController {

    @Resource
    private NoticeService noticeService;

    @ApiOperation(value = "保存通知公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeType", value = "公告类型：notice为公告；msg为消息", required = true, dataType = "NoticeType"),
            @ApiImplicitParam(name = "noticeTitle", value = "公告标题(100字符内)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "noticeContent", value = "公告内容(5000字符内)", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "tipsStartTime", value = "弹窗提示开始时间", dataType = "String"),
            @ApiImplicitParam(name = "tipsEndTime", value = "弹窗提示截止时间", dataType = "String"),
            @ApiImplicitParam(name = "dispenseOrgList", value = "下发组织数组[{orgType:'jx',orgId:组织ID,orgCode:'组织编号',orgName:'组织名称'}]", required = true, dataType = "ArrayList"),
    })
    @PostMapping("/add")
    public ResultEntity addNotice(@ApiParam("入参") @RequestBody NoticeVo params, String currentUserId) throws Exception {

        UserDto userDto = UserUtil.getCurrentUserInfo(currentUserId);
        params.setOptUser(userDto.getName());

        noticeService.saveNotice(params);
        return ResultEntity.success("新增成功");
    }

    @ApiOperation(value = "编辑公告信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "noticeType", value = "公告类型：notice为公告；msg为消息", required = true, dataType = "NoticeType"),
            @ApiImplicitParam(name = "noticeTitle", value = "公告标题(100字符内)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "noticeContent", value = "公告内容(5000字符内)", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "tipsStartTime", value = "弹窗提示开始时间", dataType = "String"),
            @ApiImplicitParam(name = "tipsEndTime", value = "弹窗提示截止时间", dataType = "String"),
            @ApiImplicitParam(name = "dispenseOrgList", value = "下发组织数组[{orgType:'jx',orgId:组织ID,orgCode:'组织编号',orgName:'组织名称'}]", required = true, dataType = "ArrayList"),
    })
    @PostMapping("/update")
    public ResultEntity updateNotice(@ApiParam(value = "入参") @RequestBody NoticeVo params, String currentUserId) throws Exception{

        UserDto userDto = UserUtil.getCurrentUserInfo(currentUserId);
        params.setOptUser(userDto.getName());

        noticeService.updateNotice(params);
        return ResultEntity.success("修改成功");
    }

    @ApiOperation(value = "删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "公告ID：id1,id2,id3", defaultValue = "", required = true, dataType = "String", paramType = "Query")
    })
    @GetMapping("/delete")
    public ResultEntity deleteNotice(String ids) throws Exception {
        noticeService.deleteNotice(ids);
        return ResultEntity.success("删除成功");
    }

    @ApiOperation(value = "查询所有公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer", defaultValue = "1", paramType = "Query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "Integer", defaultValue = "20", paramType = "Query"),
            @ApiImplicitParam(name = "noticeTitle", value = "公告标题", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "noticeContent", value = "公告内容", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "publisher", value = "发布人", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "fromTime", value = "发布开始时间", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "toTime", value = "发布截止时间", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "orgId", value = "组织ID(组织查询时传入)", dataType = "Long", paramType = "Query")
    })
    @GetMapping("/list")
    public ResultEntity<PageBean<NoticeDetailDTO>> listNotice(NoticeConditionVo params) {
        return ResultEntity.success(noticeService.findByPage(params));
    }

    @ApiOperation(value = "查看公告详情——编辑用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId", value = "公告ID", required = true, dataType = "Long", paramType = "Query")
    })
    @GetMapping("/detail")
    public ResultEntity<NoticeDTO> detailNotice(Long dataId){
        return ResultEntity.success(noticeService.getNoticeDetail(dataId));
    }

    @ApiOperation(value = "查看公告详情——预览用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId", value = "公告ID", required = true, dataType = "Long", paramType = "Query")
    })
    @GetMapping("/preview")
    public ResultEntity<NoticeInfo> previewNotice(Long dataId){
        return ResultEntity.success(noticeService.getNoticeById(dataId));
    }

}
